﻿using FSM.IDal;
using FSM.Model;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;
using Util.DataBase;

namespace FSM.Dal
{
    /// <summary>
    /// 系统设定相关处理类
    /// </summary>
    public class SysSettingDal : BaseDbContext, ISysSettingDal
    {
        #region 查询

        /// <summary>
        /// 获得用户对指定菜单拥有的操作权限按钮
        /// </summary>
        /// <param name="user_no">用户编号</param>
        /// <param name="menu_no">菜单编号</param>
        /// <returns></returns>
        public List<sys_action> ListPowerAction(string user_no, string menu_no)
        {
            return dbContext.Queryable<sys_action, rel_role_menu_action, rel_role_menu, rel_user_role>((act, rma, rm, ur) => new object[] {
                JoinType.Inner, act.act_no == rma.act_no,
                JoinType.Inner, rma.role_menu_id == rm.pkid,
                JoinType.Inner, rm.role_no == ur.role_no
            }).Where((act, rma, rm, ur) => ur.user_no == user_no && rm.menu_no == menu_no)
            .Select((act, rma, rm, ur) => act)
            .OrderBy(act => act.orderby)
            .ToList();
        }

        #endregion

        #region 判断

        /// <summary>
        /// 是否拥有指定菜单的操作权限
        /// </summary>
        /// <param name="user_no">用户编号</param>
        /// <param name="menu_no">菜单编号</param>
        /// <param name="action_no">动作编号</param>
        /// <returns></returns>
        public bool HasOperationPower(string user_no, string menu_no, string action_no)
        {
            return dbContext.Queryable<rel_user_role, rel_role_menu, rel_role_menu_action>((ur, rm, rma) => new object[] {
                JoinType.Inner, ur.role_no == rm.role_no,
                JoinType.Inner, rm.pkid == rma.role_menu_id
            }).Where((ur, rm, rma) => ur.user_no == user_no && rm.menu_no == menu_no && rma.act_no == action_no)
            .Count() > 0;
        }

        /// <summary>
        /// 是否拥有指定菜单的查看权限
        /// </summary>
        /// <param name="user_no">用户编号</param>
        /// <param name="menu_no">菜单编号</param>
        /// <returns></returns>
        public bool HasViewMenuPower(string user_no, string menu_no)
        {
            return dbContext.Queryable<rel_user_role, rel_role_menu>((ur, rm) => new object[] {
                JoinType.Inner, ur.role_no == rm.role_no
            }).Where((ur, rm) => ur.user_no == user_no && rm.menu_no == menu_no)
            .Count() > 0;
        }

        #endregion
    }
}
